# 4 - 4 - 2 神经网络的梯度
import sys, os 
sys.path.append('D:\PythonProject\DeepLearn') 
import numpy as np 
from common.functions import softmax, cross_entropy_error 
from common.gradient import numerical_gradient 

class SimpleNet: 
    def __init__(self): 
        self.W = np.random.randn(2, 3) # 权重初始化 

    def predict(self, x): 
        return np.dot(x, self.W)    
    
    def loss(self, x, t): 
        z = self.predict(x) 
        y = softmax(z) 
        loss = cross_entropy_error(y, t) 
        return loss 

def f(W): 
    return net.loss(x, t) 

if __name__ == '__main__': 
    net = SimpleNet() 
    x = np.array([0.6, 0.9]) 
    print(net.W)
    x = np.array([0.6, 0.9]) 
    p = net.predict(x) 
    print(p)
    print(np.argmax(p))
    t = np.array([0, 0, 1]) 
    print(net.loss(x, t))
    
    dw = numerical_gradient(f, net.W) 
    print(dw)